package xyz.syyrjx.blog.service.impl;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xyz.syyrjx.blog._enum.State;
import xyz.syyrjx.blog.dao.blog.BlogDao;
import xyz.syyrjx.blog.entity.Blog;
import xyz.syyrjx.blog.exception.BlogException;
import xyz.syyrjx.blog.service.BlogService;
import xyz.syyrjx.blog.util.ConstValuePool;
import xyz.syyrjx.blog.util.site.BingSubmitUtil;

import javax.annotation.Resource;
import java.util.*;

@Service
public class BlogServiceImpl implements BlogService {
    @Resource
    private BlogDao dao;

    @Override
    public int countBlogs(String search, Long type) {
        Blog template = new Blog();
        if (search != null && search.length() > 0) {
            template.setTitle(search);
        }
        template.setType(type);
        template.setState(State.NORMAL_STATE.getCode());
        return dao.countBlogs(template);
    }

    @Override
    public List<Blog> queryBlogs(Blog template,Integer page) {
        Integer start = null;
        Integer num = null;
        if (page == null || page == 0){
            page = 1;
        }
        if (page != null){
            start = (page - 1) * 5;
            num = 5;
        }
        template.setState(State.NORMAL_STATE.getCode());
        return dao.selectBlogs(template,start,num);
    }

    @Override
    public List<Blog> queryAllBlogs() {
        Blog template = new Blog();
        template.setState(State.NORMAL_STATE.getCode());
        return dao.selectBlogs(template,null,null);
    }

    @Override
    public Map<String, List<Blog>> getArchives() {
        List<Blog> blogList = queryAllBlogs();
        List<String> dateList = new ArrayList<>();
        Map<String,List<Blog>> DateAndBlog = new HashMap<>();
        for(Blog blog:blogList){
            String time = ((Integer)(blog.getCreateDate().getYear() + 1900)).toString();
            boolean dateHave = false;
            for(String date:dateList){
                if(time.equals(date)){
                    dateHave = true;
                    break;
                }
            }
            if(!dateHave){
                dateList.add(time);
                DateAndBlog.put(time,new LinkedList<>());
            }
            DateAndBlog.get(time).add(blog);
        }
        return DateAndBlog;
    }


    @Override
    public Blog queryAndConvert(Long id) {
        Blog template = new Blog();
        template.setId(id);
        template.setState(State.NORMAL_STATE.getCode());
        Blog blog = dao.selectBlogs(template,null,null).get(0);
        String content = blog.getContent();
        blog.setContent(content);
        return blog;
    }

    @Override
    public List<Blog> queryBlogInPage(Integer page) {
        Blog template = new Blog();
        template.setState(State.NORMAL_STATE.getCode());
        return dao.selectBlogs(template ,(page - 1) * 5,5);
    }

    @Override
    public List<Blog> queryBlogByTypeInPage(Integer page, Long typeId) {
        List<Blog> blogList = null;
        if(typeId == null){
            blogList = queryBlogInPage(page);
        }else {
            if (page <= 0){
                page = 1;
            }
            Blog template = new Blog();
            template.setType(typeId);
            template.setState(State.NORMAL_STATE.getCode());
            blogList = dao.selectBlogs(template,(page - 1) * 5,5);
        }
        return blogList;
    }

    @Override
    public List<Blog> likeQueryBlogInPage(Integer page, String search) {
        Blog template = new Blog();
        template.setTitle(search);
        template.setState(State.NORMAL_STATE.getCode());
        return dao.selectBlogs(template,(page - 1) * 5,5);
    }

    @Override
    public void addBlog(Blog blog) {
        // 查询博客标题是否重复
        Blog titleQuery = new Blog();
        titleQuery.setTitle(blog.getTitle());
        List<Blog> blogs = dao.selectBlogs(titleQuery, null, null);
        if (blogs.size() > 0) {
            throw new BlogException("博客标题重复");
        }

        // 添加博客
        Date nowTime = new Date();
        blog.setView(0);
        blog.setState(State.NORMAL_STATE.getCode());
        blog.setCreateDate(nowTime);
        blog.setUpdateDate(nowTime);
        dao.insertBlog(blog);

        // 提交url到bing
        Blog idBlog = dao.selectBlogs(titleQuery, null, null)
                .stream()
                .findFirst()
                .orElseThrow(() -> new BlogException("博客插入成功，但未查询到博客，请手动提交博客url"));
        List<String> urlList = new ArrayList<>();
        String newBlogUrl = ConstValuePool.BLOG_HOST + "/blog?id=" + idBlog.getId();
        urlList.add(newBlogUrl);
        BingSubmitUtil.submit(urlList);
    }

    @Override
    @Transactional
    public boolean deleteBlogById(Long id) {
        boolean flag = false;
        int update = 0;
        if (dao.deleteBlogById(id) > 0) {
            flag = true;
        }
        return flag;
    }

    @Override
    public boolean updateBlog(Blog blog) {
        boolean flag = false;
        if(dao.updateBlog(blog) > 0){
            flag = true;
        }
        return flag;
    }

    @Override
    public boolean updateView(Blog blog) {
        boolean flag = false;
        if(dao.updateView(blog) > 0){
            flag = true;
        }
        return flag;
    }
}
